home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / GSRC208A.ZIP / IOSIM.C < prev    next >
C/C++ Source or Header  |  1993-07-14  |  15KB  |  560 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*         GWSIM - Simulation        */
  11. /*        MS-WINDOWS front end       */
  12. /*                                   */
  13. /*        Simulation files I/O       */
  14. /*                                   */
  15. /*          QuickC/WIN 1.0           */
  16. /*                                   */
  17. /*   (include here compilers that    */
  18. /*   compiled GWSIM successfully)    */
  19. /*                                   */
  20. /*************************************/
  21.  
  22.  
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <io.h>
  28. #include <sys\types.h>
  29. #include <sys\stat.h>
  30. #include "commdlg.h"
  31. #include "globals.h"
  32. #include "iotop.h"
  33. #include "iosim.h"
  34. #include "gep2.h"
  35. #include "simgvar.h"
  36. #include "gaussw.h"
  37. #include "strtbl.h"
  38.  
  39. #pragma alloc_text( CODE13, TopIn, SimToBuf, WriteSim, BufToSim, ReadSim, setdb )
  40.  
  41. /*
  42.     set up several database variables
  43. */
  44. int setdb( int swapnames )
  45. {
  46.  /* set up the strings describing each step                            */
  47.  step_string( );
  48.  
  49.  /* reduce the stoicheiometry matrix                                */
  50.  if( reduce( swapnames ) != 0 )
  51.   return -1;
  52.  
  53.  /* if there are no steps with internal metabolites signal and stop    */
  54.  if( (!nsteps) || (!nmetab) )
  55.   return IDS_ERR_BAD_EMPTY;
  56.  /* setup the parameters structure                                    */
  57.  if( SetParams() == -1 )
  58.   return IDS_ERR_NOEXEC;
  59.  
  60.  /* setup the outpel structure                                        */
  61.  if( SetOutpEl() == -1 )
  62.   return IDS_ERR_NOEXEC;
  63.  
  64.  return 0;
  65. }
  66.  
  67. /*
  68.     read topology file with validity check
  69. */
  70.  
  71. int TopIn( LPSTR tname )
  72. {
  73.  unsigned char  nmod;
  74.  BOOL            badtop;
  75.  int            i, j, nRc;
  76.  
  77.  if( !(nRc = ReadTop( tname ) ) )
  78.  {
  79.   badtop = FALSE;
  80.   if( (nsteps<=0) || (nsteps>MAX_STEP) ||
  81.       (totmet<=0) || (totmet>MAX_MET)    )
  82.    badtop = TRUE;
  83.   /* check if topology is complete                            */
  84.   for( i=0; i<nsteps; i++ )
  85.   {
  86.    if( kinetu[i]==NOT ) badtop = TRUE;
  87.    if( ktype[kinetu[i]].nmodf > 0 )
  88.    {
  89.     /* count the number of assigned modfs for each reaction    */
  90.     for( j=0, nmod=0; j<totmet; j++ )
  91.      if( (*loop)[i][j] != 0 ) nmod++;
  92.     if( ktype[kinetu[i]].nmodf != nmod ) badtop = TRUE;
  93.    }
  94.   }
  95.  
  96.   /* if topology is incomplete, signal and return                */
  97.   if( badtop )
  98.   {
  99.    tname[0] = '\0';
  100.    return IDS_ERR_BAD_TOP;
  101.   }
  102.  
  103.   nRc = setdb( 1 ); /* 1 means swap metabolite and step names    */
  104.  
  105.   /* exit and signal no errors                                */
  106.   return nRc;
  107.  }
  108.  else return IDS_ERR_LOAD;
  109. }
  110.  
  111. /*
  112.     write simulation parameters to file
  113. */
  114.  
  115. void SimToBuf( LPSTR Buff, LPSTR FName )
  116. {
  117.  int i, j;
  118.  char *p;
  119.  char auxstr[260];
  120.  
  121.  /* write the kinetic constants, one line per step                        */
  122.  for( i=0; i<nsteps; i++ )
  123.  {
  124.   for( j=0; j<(int)ktype[kinetu[i]].nconst; j++ )
  125.   {
  126.    lstrcat( Buff, (LPSTR) " " );
  127.    gcvt( *(params[i]+j), 20, auxstr );
  128.    lstrcat( Buff, (LPSTR) auxstr );
  129.   }
  130.   lstrcat( Buff, (LPSTR) "\n" );
  131.  }
  132.  
  133.  /* write the concentrations, one per line                                */
  134.  for( i=0; i<totmet; i++ )
  135.  {
  136.   gcvt( xu[i], 20, auxstr );
  137.   lstrcat( Buff, (LPSTR) auxstr );
  138.   lstrcat( Buff, (LPSTR) "\n" );
  139.  }
  140.  
  141.  /* write the units in one  line                                        */
  142.  wsprintf( (LPSTR) auxstr, "%s %s\n", (LPSTR) options.concu, (LPSTR) options.timeu );
  143.  lstrcat( Buff, (LPSTR) auxstr );
  144.  /* write log, dyn and ss options in one line                            */
  145.  sprintf( auxstr, "%d %d %lu %.20le %.20le %.20le %.20le %d %d %d\n",
  146.           options.debug, options.dyn, options.pfo, options.endtime,
  147.           options.reltol, options.abstol, options.hrcz, options.adams,
  148.           options.bdf, options.ss);
  149.  lstrcat( Buff, (LPSTR) auxstr );
  150.  /* write txt options in one line                                        */
  151.  sprintf( auxstr, "%d %d %d %d %d %d %d\n", options.txt,
  152.           options.structan, options.staban, options.stdela,
  153.           options.nonela, options.stdcc, options.noncc );
  154.  lstrcat( Buff, (LPSTR) auxstr );
  155.  /* write the dat options in one line                                    */
  156.  sprintf( auxstr, "%d %d %d %d %d %d %d %d %d\n", options.dat, options.datsep,
  157.           options.datwidth, options.dattit, options.datmca, options.datss,
  158.           totsel, options.append, options.quotes );
  159.  lstrcat( Buff, (LPSTR) auxstr );
  160.  /* write the dat file title                                            */
  161.  sprintf( auxstr, "%s\n", DatName );
  162.  lstrcat( Buff, (LPSTR) auxstr );
  163.  /* write each output item in one line (index and title)                */
  164.  for( i=0; i<totsel; i++ )
  165.   for( j=0; j<noutpel; j++ )
  166.    if( outpel[j].idx == (unsigned int) (i+1) )
  167.    {
  168.     wsprintf( (LPSTR) auxstr, "%d %s\n", j, outpel[j].title );
  169.     lstrcat( Buff, (LPSTR) auxstr );
  170.    }
  171.  /* write the scan options in one line                                    */
  172.  sprintf( auxstr, "%d %lu %d %d %d\n", options.scan, options.scandens,
  173.           options.scanlog, totscan, nlinks );
  174.  lstrcat( Buff, (LPSTR) auxstr );
  175.  /* write each scan item in one line                                      */
  176.  for( i=0; i<totscan; i++ )
  177.   for( j=0; j<nscanpar; j++ )
  178.    if( spar[j].idx == (i+1) )
  179.    {
  180.     sprintf( auxstr, "%d %.20le %.20le %lu %d\n", j, spar[j].low,
  181.              spar[j].high, spar[j].dens, spar[j].log );
  182.     lstrcat( Buff, (LPSTR) auxstr );
  183.    }
  184.  /* write each linked item in one line                                    */
  185.  for( i=0; i<nlinks; i++ )
  186.   for( j=0; j<nscanpar; j++ )
  187.    if( spar[j].lidx == i )
  188.    {
  189.     sprintf( auxstr, "%d %d %d %20le\n", j, spar[j].linkedto,
  190.              spar[j].operation, spar[j].factor );
  191.     lstrcat( Buff, (LPSTR) auxstr );
  192.    }
  193. }
  194.  
  195.  
  196. int WriteSim( LPSTR FName )
  197. {
  198.  HCURSOR hSaveCursor;
  199.  GLOBALHANDLE hBuff;
  200.  LPSTR Buff;
  201.  int i, j, ch1;
  202.  WORD bufsize;
  203.  OFSTRUCT OfStruct;
  204.  
  205.  /* load the wait cursor                                                */
  206.  hSaveCursor = SetCursor(hHourGlass);
  207.  
  208.  /*bufsize = (WORD) (3*nsteps + (8*nsteps + 6 + NAME_L)*totmet);        */
  209.  bufsize = (WORD) 10240;
  210.  hBuff = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT , bufsize );
  211.  Buff = GlobalLock( hBuff );
  212.  /* write the information on the buffer                                    */
  213.  TopToBuf( Buff );
  214.  if( (ch1 = OpenFile( FName, &OfStruct, OF_CREATE | OF_WRITE )) != -1 )
  215.  {
  216.   /* write the buffer to the file and close it                            */
  217.   _lwrite( ch1, (LPSTR) Buff, (WORD) lstrlen( Buff ) );
  218.   ch1 = _lclose( ch1 );
  219.  }
  220.  if( ch1 == -1 )
  221.  {
  222.   GlobalUnlock( hBuff );
  223.   GlobalFree( hBuff );
  224.   SetCursor(hSaveCursor);
  225.   return IDS_ERR_SAVE;
  226.  }
  227.  
  228.  /* set the flags, meaning 'kinetic type not saved' */
  229.  for( i=0; i<nsteps; i++ ) kfl[i] = 1;
  230.  /* check if model contains user-defined kinetic types                    */
  231.  if( nudf>0 )
  232.  {
  233.   for( i=0; i<nsteps; i++ )
  234.    if( (kinetu[i] > MAX_TYP-1) && (kfl[i]) )
  235.    {
  236.     /* write the tree function to the buffer                                */
  237.     TreeToBuf( kinetu[i]-MAX_TYP, (LPSTR) Buff );
  238.     /* reopen the file and write this tree to it                            */
  239.     if( (ch1 = OpenFile( (LPSTR) NULL, &OfStruct, OF_REOPEN | OF_WRITE )) != -1 )
  240.     {
  241.      /* write the buffer to the file and close it                            */
  242.      _lseek( ch1, (LONG) 0, 2 );
  243.      _lwrite( ch1, (LPSTR) Buff, (WORD) lstrlen( Buff ) );
  244.      ch1 = _lclose( ch1 );
  245.     }
  246.     if( ch1 == -1 )
  247.     {
  248.      GlobalUnlock( hBuff );
  249.      GlobalFree( hBuff );
  250.      SetCursor(hSaveCursor);
  251.      return IDS_ERR_SAVE;
  252.     }
  253.     /* mark all other reactions with this type */
  254.     for( j=i; j<nsteps; j++ ) if(kinetu[j]==kinetu[i]) kfl[j] = 0;
  255.    }
  256.  }
  257.  
  258.  
  259.  Buff[0] = '\0';
  260.  SimToBuf( Buff, FName );
  261.  /* append buffer to  the file                                            */
  262.  if( (ch1 = OpenFile( (LPSTR) NULL, &OfStruct, OF_REOPEN | OF_WRITE )) != -1 )
  263.  {
  264.   /* write the buffer to the file and close it                            */
  265.   _lseek( ch1, (LONG) 0, 2 );
  266.   _lwrite( ch1, (LPSTR) Buff, (WORD) lstrlen( Buff ) );
  267.    ch1 = _lclose( ch1 );
  268.  }
  269.  if( ch1 == -1 )
  270.  {
  271.   GlobalUnlock( hBuff );
  272.   GlobalFree( hBuff );
  273.   SetCursor(hSaveCursor);
  274.   return IDS_ERR_SAVE;
  275.  }
  276.  
  277.  /* release the memory block                                            */
  278.  GlobalUnlock( hBuff );
  279.  GlobalFree( hBuff );
  280.  SetCursor(hSaveCursor);
  281.  return 0;
  282. }
  283.  
  284.  
  285. /*
  286.     read buffer with simulation parameters and store them
  287. */
  288.  
  289. int BufToSim( char *Buff )
  290. {
  291.  int i, j, log, lkt, op;
  292.  unsigned